package RentCarProject;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.support.spring.DruidNativeJdbcExtractor;

import javax.imageio.ImageIO;
import javax.sql.DataSource;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;

public class RentCarUI {
    JFrame frame;
    MyButton rentbutton,returnbutton,paybutton,changbutton;
    JMenuBar menuBar;
    JMenu tmenu;
    JMenuItem menu,menu1,menu2,menu3,menu4,menu5,menu6,menu7,menu8,menu9;
    public JLabel label,carlabel;
    JPanel buttonpanel;
    JTable table=new JTable();
    JScrollPane scrollPane;
    JSplitPane splitPane,usplitPane;
    ArrayList<Car> carList=new ArrayList<>();
    Object id;
    UserBase userBase;
    public int rentaccount=0;
    public void setUserbase(UserBase userBase) {
        this.userBase=userBase;
    }
    public void init() throws Exception {
        frame=new JFrame("欢迎"+userBase.getUsername()+"！");
        frame.setSize(1000,1000);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //菜单条
        menuBar=new JMenuBar();
        tmenu=new JMenu("查找");
        menu=new JMenuItem("总览");
        menu.setPreferredSize(new Dimension(100,20));
        menu1=new JMenuItem("租出");
        menu1.setPreferredSize(new Dimension(100,20));
        menu2=new JMenuItem("待租");
        menu2.setPreferredSize(new Dimension(100,20));
        menu3=new JMenuItem("热度排行");
        menu3.setPreferredSize(new Dimension(100,20));
        menu4=new JMenuItem("租车记录");
        menu4.setPreferredSize(new Dimension(100,20));
        menu5=new JMenuItem("充值记录");
        menu5.setPreferredSize(new Dimension(100,20));
        menu6=new JMenuItem("通过id查找");
        menu6.setPreferredSize(new Dimension(100,20));
        menu7=new JMenuItem("通过车名查找");
        menu7.setPreferredSize(new Dimension(100,20));
        menu8=new JMenuItem("通过颜色查找");
        menu8.setPreferredSize(new Dimension(100,20));
        menu9=new JMenuItem("通过车型查找");
        menu9.setPreferredSize(new Dimension(100,20));
        tmenu.add(menu6);
        tmenu.add(menu7);
        tmenu.add(menu8);
        tmenu.add(menu9);
        menuBar.add(menu);
        menuBar.add(menu1);
        menuBar.add(menu2);
        menuBar.add(menu3);
        menuBar.add(menu4);
        menuBar.add(menu5);
        menuBar.add(tmenu);
        menuBar.setBounds(0,0,1000,20);

        menu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    refreshTabel();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        menu1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    isrentTabel();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        menu2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    notrentTabel();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        menu3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    sortTabel();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        menu4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    reportTabel();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        menu5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    chargeTabel();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        menu6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object searchid=null;
                searchid=JOptionPane.showInputDialog(frame,"输入要查找的车辆id：");
                if(searchid!=null){
                    try {
                        refreshTablebyid(searchid);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });
        menu7.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object searchname=null;
                searchname=JOptionPane.showInputDialog(frame,"输入要查找的车辆名称：");
                if(searchname!=null){
                    try {
                        refreshTablebyname(searchname);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });
        menu8.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object searchcolor=null;
                searchcolor=JOptionPane.showInputDialog(frame,"输入要查找的车辆颜色：");
                if(searchcolor!=null){
                    try {
                        refreshTablebycolor(searchcolor);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });
        menu9.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object searchtype=null;
                searchtype=JOptionPane.showInputDialog(frame,"输入要查找的车辆类型：");
                if(searchtype!=null){
                    try {
                        refreshTablebytype(searchtype);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });

        label=new JLabel("账户余额："+userBase.account);
        label.setBounds(50,0,100,30);

        table.setFont(new Font("宋体",Font.BOLD,14));
        table.setRowHeight(25);
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
        table.setDefaultRenderer(Object.class, centerRenderer);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount()==1){
                    int row=table.getSelectedRow();
                    id=table.getModel().getValueAt(row,0);
                    try {
                        carlabel.setIcon(new ImageIcon(searchPictureAddress(id)));
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });

        refreshTabel();

        carlabel=new JLabel();
        carlabel.setIcon(new ImageIcon("src/picture/start.png"));

        rentbutton=new MyButton("租赁");
        rentbutton.setBounds(0,40,170,80);
        rentbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if(id!=null&&searchStatusbyid(id)){
                        new rentCarFrame();
                    }else if(id==null){
                        JOptionPane.showMessageDialog(frame,"请在车辆栏中选择要租赁的车辆！");
                    }else{
                        JOptionPane.showMessageDialog(frame,"该车已被租出！");
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        returnbutton=new MyButton("还车");
        returnbutton.setBounds(0,130,170,80);
        returnbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    new returnCarFrame();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        paybutton=new MyButton("充值");
        paybutton.setBounds(0,220,170,80);
        paybutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String a=JOptionPane.showInputDialog(frame,"输入要充值的金额：");
                if(a!=null){
                    int add = Integer.parseInt(a);
                    try {
                        userBase.addAccount(add);
                        userBase.userCharge(add);
                        label.setText("账户余额：" + userBase.account);
                        JOptionPane.showMessageDialog(frame, "充值成功！");
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });

        changbutton=new MyButton("切换账号");
        changbutton.setBounds(0,310,170,80);
        changbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new LoginUI();
            }
        });

        buttonpanel=new JPanel();
        buttonpanel.setLayout(null);
        buttonpanel.add(label);
        buttonpanel.add(rentbutton);
        buttonpanel.add(returnbutton);
        buttonpanel.add(paybutton);
        buttonpanel.add(changbutton);

        scrollPane=new JScrollPane(table);
        usplitPane=new JSplitPane();
        usplitPane.setEnabled(false);
        usplitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
        usplitPane.setDividerLocation(800);
        usplitPane.setLeftComponent(scrollPane);
        usplitPane.setRightComponent(buttonpanel);
        splitPane=new JSplitPane();
        splitPane.setEnabled(false);
        splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
        splitPane.setDividerLocation(400);
        splitPane.setLeftComponent(usplitPane);;
        splitPane.setRightComponent(carlabel);

        frame.add(menuBar,BorderLayout.NORTH);
        frame.add(splitPane,BorderLayout.CENTER);
        frame.setVisible(true);
    }

    public class rentCarFrame extends JFrame{
        JLabel thislabel,rentcarlabel;
        MyButton button1,button2;
        public rentCarFrame() {
            try {
                setTitle(getCarName(id));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            setLayout(null);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setSize(400,250);
            setLocationRelativeTo(null);

            rentcarlabel=new JLabel(new ImageIcon("src/picture/rentcarbg.png"));
            rentcarlabel.setBounds(0,0,400,250);
            rentcarlabel.setOpaque(false);
            thislabel = new JLabel("请选择要租赁的天数：");
            thislabel.setForeground(Color.white);
            thislabel.setFont(new Font("宋体",Font.BOLD,15));
            thislabel.setBounds(120,10,300,50);
            SpinnerNumberModel spinnerModel = new SpinnerNumberModel(1, 1, 100, 1);
            JSpinner spinner = new JSpinner(spinnerModel);
            spinner.setBounds(150,70,100,30);
            button1=new MyButton("租赁");
            button1.setBounds(50,130,100,40);
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        int days=Integer.parseInt(spinner.getValue().toString());
                        rentaccount=searchRent(id)*days;
                        if(userBase.getAccount()>=rentaccount){
                            rentCarbyid(id,days);
                            userBase.minAccount(rentaccount);
                            refreshTabel();
                            addCount(id);
                            addRentReport(id);
                            label.setText("账户余额：" + userBase.account);
                            JOptionPane.showMessageDialog(frame,"租车成功！租车密钥为："+id);
                        }else {
                            JOptionPane.showMessageDialog(frame,"账户余额不足！");
                        }
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });
            button2=new MyButton("取消");
            button2.setBounds(250,130,100,40);
            button2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });
            add(thislabel);
            add(spinner);
            add(button1);
            add(button2);
            add(rentcarlabel);
            setVisible(true);
        }
    }

    public class returnCarFrame extends JFrame{
        JLabel thislabel,returncarlabel;
        JTextField textfield;
        MyButton button1,button2;
        public returnCarFrame() {
            setLayout(null);
            setTitle("用户还车");
            setSize(400,250);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            returncarlabel=new JLabel(new ImageIcon("src/picture/returncarbg.png"));
            returncarlabel.setOpaque(false);
            returncarlabel.setBounds(0,0,400,250);
            thislabel = new JLabel("请输入租车密钥");
            thislabel.setForeground(Color.WHITE);
            thislabel.setBounds(145,30,200,40);
            thislabel.setFont(new Font("宋体",Font.BOLD,15));
            textfield=new JTextField();
            textfield.setBounds(160,70,80,30);

            button1=new MyButton("还车");
            button1.setBounds(80,130,100,40);
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Object returnid=textfield.getText();
                    if(returnid==null){
                        JOptionPane.showMessageDialog(null,"车辆密钥为空！");
                    }else{
                        try {
                            if(searchCarbyid(returnid)){
                                if(!searchStatusbyid(returnid)){
                                    if(isuserRentcar(returnid)){
                                        if(rentDays(returnid)<=initialDays(returnid)){
                                            returnCarbyid(returnid);
                                            addReturnReport(returnid);
                                            refreshTabel();
                                            JOptionPane.showMessageDialog(null, "还车成功！");
                                        }else{
                                            int exceedrent=getRent(returnid)*(rentDays(returnid)-initialDays(returnid));
                                            int result=JOptionPane.showConfirmDialog(null,"由于该车逾期未还，现需支付逾期租赁费用"+exceedrent+"元。是否支付并还车？","确认还车",JOptionPane.YES_NO_OPTION);
                                            if(result==JOptionPane.YES_OPTION){
                                                if(userBase.getAccount()>=exceedrent){
                                                    returnCarbyid(returnid);
                                                    addReturnReport(returnid);
                                                    userBase.minAccount(exceedrent);
                                                    refreshTabel();
                                                    label.setText("账户余额：" + userBase.account);
                                                    JOptionPane.showMessageDialog(null, "还车成功！");
                                                }else{
                                                    JOptionPane.showMessageDialog(null,"账户余额不足！");
                                                }
                                            }else{
                                                JOptionPane.showMessageDialog(null,"还车失败！");
                                            }
                                        }
                                    }else{
                                        JOptionPane.showMessageDialog(null,"还车失败！");
                                    }
                                }else {
                                    JOptionPane.showMessageDialog(null,"该车未租出！");
                                }
                            }else{
                                JOptionPane.showMessageDialog(null,"车库中没有这辆车！");
                            }
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
            });
            button2=new MyButton("退出");
            button2.setBounds(220,130,100,40);
            button2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });

            add(thislabel);
            add(textfield);
            add(button1);
            add(button2);
            add(returncarlabel);
            setVisible(true);
        }
    }

    public void addRentReport(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="insert ignore into rentreport(user,carid,carname,carcolor,cartype,renttime,returntime,days,account,picture) select renter,id,name,color,type,renttime,returntime,days,days*rent,picture from car where id=?";
        PreparedStatement ps=connection.prepareStatement(sql);
        ps.setString(1, String.valueOf(id));
        ps.executeUpdate();
        ps.close();
        connection.close();
    }

    public void addReturnReport(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="update rentreport set returntime=current_time where carid=? and user=?";
        PreparedStatement ps=connection.prepareStatement(sql);
        ps.setString(1, String.valueOf(id));
        ps.setString(2, userBase.getUsername());
        ps.executeUpdate();
        ps.close();
        connection.close();
    }

    public void refreshTabel() throws Exception {
        carList.clear();
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car";
        ResultSet resultSet=connection.createStatement().executeQuery(sql);
        while(resultSet.next()){
            Car car=new Car();
            car.setId(resultSet.getInt("id"));
            car.setName(resultSet.getString("name"));
            car.setColor(resultSet.getString("color"));
            car.setType(resultSet.getString("type"));
            car.setState(resultSet.getBoolean("status"));
            car.setRent(resultSet.getInt("rent"));
            carList.add(car);
        }
        resultSet.close();
        connection.close();
        DefaultTableModel model=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","状态","租金（元/天）"},0);
        for(Car car:carList){
            model.addRow(new Object[]{car.getId(),car.getName(),car.getColor(),car.getType(),statusToString(car.getState()),car.getRent()});
        }
        table.setModel(model);
    }

    public void refreshTablebyid(Object id) throws Exception {
        DefaultTableModel model=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","状态","租金（元/天）"},0);
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where id=?";
        PreparedStatement ps=connection.prepareStatement(sql);
        ps.setString(1, String.valueOf(id));
        ResultSet resultSet=ps.executeQuery();
        while(resultSet.next()){
            model.addRow(new Object[]{resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("color"), resultSet.getString("type"), statusToString(resultSet.getBoolean("status")), resultSet.getInt("rent")});
        }
        resultSet.close();
        connection.close();
        table.setModel(model);
    }

    public void refreshTablebyname(Object name) throws Exception {
        DefaultTableModel model=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","状态","租金（元/天）"},0);
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where name=?";
        PreparedStatement ps=connection.prepareStatement(sql);
        ps.setString(1, String.valueOf(name));
        ResultSet resultSet=ps.executeQuery();
        while(resultSet.next()){
            model.addRow(new Object[]{resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("color"), resultSet.getString("type"), statusToString(resultSet.getBoolean("status")), resultSet.getInt("rent")});
        }
        resultSet.close();
        connection.close();
        table.setModel(model);
    }

    public void refreshTablebycolor(Object color) throws Exception {
        DefaultTableModel model=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","状态","租金（元/天）"},0);
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where color=?";
        PreparedStatement ps=connection.prepareStatement(sql);
        ps.setString(1, String.valueOf(color));
        ResultSet resultSet=ps.executeQuery();
        while(resultSet.next()){
            model.addRow(new Object[]{resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("color"), resultSet.getString("type"), statusToString(resultSet.getBoolean("status")), resultSet.getInt("rent")});
        }
        resultSet.close();
        connection.close();
        table.setModel(model);
    }

    public void refreshTablebytype(Object type) throws Exception {
        DefaultTableModel model=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","状态","租金（元/天）"},0);
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where type=?";
        PreparedStatement ps=connection.prepareStatement(sql);
        ps.setString(1, String.valueOf(type));
        ResultSet resultSet=ps.executeQuery();
        while(resultSet.next()){
            model.addRow(new Object[]{resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("color"), resultSet.getString("type"), statusToString(resultSet.getBoolean("status")), resultSet.getInt("rent")});
        }
        resultSet.close();
        connection.close();
        table.setModel(model);
    }

    public void notrentTabel() throws Exception {
        carList.clear();
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where status=1";
        ResultSet resultSet=connection.createStatement().executeQuery(sql);
        while(resultSet.next()){
            Car car=new Car();
            car.setId(resultSet.getInt("id"));
            car.setName(resultSet.getString("name"));
            car.setColor(resultSet.getString("color"));
            car.setType(resultSet.getString("type"));
            car.setRent(resultSet.getInt("rent"));
            carList.add(car);
        }
        resultSet.close();
        connection.close();
        DefaultTableModel model1=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","租金(元/天)"},0);
        for(Car car:carList){
            model1.addRow(new Object[]{car.getId(),car.getName(),car.getColor(),car.getType(),car.getRent()});
        }
        table.setModel(model1);
    }

    public void isrentTabel() throws Exception {
        carList.clear();
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where status=0";
        ResultSet resultSet=connection.createStatement().executeQuery(sql);
        while(resultSet.next()){
            Car car=new Car();
            car.setId(resultSet.getInt("id"));
            car.setName(resultSet.getString("name"));
            car.setColor(resultSet.getString("color"));
            car.setType(resultSet.getString("type"));
            car.setRent(resultSet.getInt("rent"));
            carList.add(car);
        }
        resultSet.close();
        connection.close();
        DefaultTableModel model2=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","租金(元/天)"},0);
        for(Car car:carList){
            model2.addRow(new Object[]{car.getId(),car.getName(),car.getColor(),car.getType(),car.getRent()});
        }
        table.setModel(model2);
    }

    public void sortTabel() throws Exception {
        carList.clear();
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car order by count desc";
        ResultSet resultSet=connection.createStatement().executeQuery(sql);
        while(resultSet.next()){
            Car car=new Car();
            car.setId(resultSet.getInt("id"));
            car.setName(resultSet.getString("name"));
            car.setColor(resultSet.getString("color"));
            car.setType(resultSet.getString("type"));
            car.setState(resultSet.getBoolean("status"));
            car.setRent(resultSet.getInt("rent"));
            car.setCount(resultSet.getInt("count"));
            carList.add(car);
        }
        resultSet.close();
        connection.close();
        DefaultTableModel model=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","状态","租金(元/天)","租赁次数"},0);
        for(Car car:carList){
            model.addRow(new Object[]{car.getId(),car.getName(),car.getColor(),car.getType(),statusToString(car.getState()),car.getRent(),car.getCount()});
        }
        table.setModel(model);
    }

    public void reportTabel() throws Exception {
        DefaultTableModel model=new DefaultTableModel(new Object[]{"编号","名称","颜色","车型","租赁时间","还车时间","租赁天数","支付金额"},0);
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from rentreport where user=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,userBase.getUsername());
        ResultSet resultSet=preparedStatement.executeQuery();
        while(resultSet.next()){
            model.addRow(new Object[]{resultSet.getInt("carid"),resultSet.getString("carname"),resultSet.getString("carcolor"),resultSet.getString("cartype"),resultSet.getString("renttime"),resultSet.getString("returntime"),resultSet.getInt("days"),resultSet.getInt("account")});
        }
        table.setModel(model);
    }

    public void chargeTabel() throws Exception {
        DefaultTableModel model=new DefaultTableModel(new Object[]{"充值日期","充值金额"},0);
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from charge where user=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,userBase.getUsername());
        ResultSet resultSet=preparedStatement.executeQuery();
        while(resultSet.next()){
            model.addRow(new Object[]{resultSet.getString("chargetime"),resultSet.getInt("account")});
        }
        table.setModel(model);
    }

    public boolean searchStatusbyid(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select status from car where id=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,String.valueOf(id));
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()){
            boolean a=resultSet.getBoolean("status");
            return a;
        }else {
            return false;
        }
    }

    public boolean searchCarbyid(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where id=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setObject(1,id);
        ResultSet resultSet=preparedStatement.executeQuery();
        return resultSet.next();
    }

    public boolean isuserRentcar(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where id=? and renter=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,String.valueOf(id));
        preparedStatement.setString(2,userBase.getUsername());
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()){
            return true;
        }else {
            return false;
        }
    }

    public int initialDays(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where id=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,String.valueOf(id));
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()){
            int days=resultSet.getInt("days");
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return days;
        }else{
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return 0;
        }
    }

    public int rentDays(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="SELECT TIMESTAMPDIFF(DAY,renttime,CURRENT_TIME) as rentdays FROM car WHERE id=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,String.valueOf(id));
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()){
            int a=resultSet.getInt("rentdays");
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return a;
        }else {
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return 0;
        }
    }

    public int getRent(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where id=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,String.valueOf(id));
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()){
            int a=resultSet.getInt("rent");
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return a;
        }else{
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return 0;
        }
    }

    public void rentCarbyid(Object id,int days) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql1="update car set status=0,renttime=current_time,renter=?,days=? where id=?";
        PreparedStatement preparedStatement1=connection.prepareStatement(sql1);
        preparedStatement1.setString(1,userBase.getUsername());
        preparedStatement1.setString(2,String.valueOf(days));
        preparedStatement1.setString(3,String.valueOf(id));
        preparedStatement1.executeUpdate();
        preparedStatement1.close();
        connection.close();
    }

    public void returnCarbyid(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="update car set status=1,returntime=current_time,renter=null,days=null where id=?";
        PreparedStatement preparedStatement1=connection.prepareStatement(sql);
        preparedStatement1.setString(1,String.valueOf(id));
        preparedStatement1.executeUpdate();
        preparedStatement1.close();
        connection.close();
    }

    public int searchRent(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where id=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,String.valueOf(id));
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()){
            int a=resultSet.getInt("rent");
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return a;
        }else{
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return 0;
        }
    }

    public void addCount(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="update car set count=count+1 where id=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,String.valueOf(id));
        preparedStatement.executeUpdate();
        preparedStatement.close();
        connection.close();
    }

    public String searchPictureAddress(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where id=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,String.valueOf(id));
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()&&resultSet.getString("picture")!=null){
            String a=resultSet.getString("picture");
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return a;
        }else{
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return "error.png";
        }
    }

    public String getCarName(Object id) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from car where id=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,String.valueOf(id));
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()){
            String a=resultSet.getString("name");
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return a;
        }else {
            return null;
        }
    }

    public String statusToString(boolean status){
        if(status){
            return "待租";
        }else {
            return "已租出";
        }
    }

}
